package com.cloud.sysmana.dto;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.cloud.sysmana.dto.FuncTree;
import com.cloud.sysmana.entity.SysFunc;
import com.cloud.sysmana.ser.ISysFuncService;

/**
 * 实现系统功能的树能结构
 * 
 * @version 1.0
 * @date 2012-09-01
 * @author DWZ框架组
 */
public class FuncTree {

	private SysFunc func;// 功能信息
	private List<FuncTree> childFunc;// 下级功能信息

	public SysFunc getFunc() {
		return func;
	}

	public void setFunc(SysFunc func) {
		this.func = func;
	}

	public List<FuncTree> getChildFunc() {
		return childFunc;
	}

	public void setChildFunc(List<FuncTree> childFunc) {
		this.childFunc = childFunc;
	}

	/**
	 * 列表构建菜单[只有模块和菜单信息]
	 * 
	 * @param funcTree
	 *            ：功能树对象
	 * @param funcList
	 *            ：系统功能列表
	 */
	public static void listFuncMenu(FuncTree funcTree,
			List<SysFunc> funcMenuList) {

		SysFunc func = funcTree.getFunc();

		for (FuncTree tree : funcTree.getChildFunc()) {
			func = tree.getFunc();
			if (ISysFuncService.FUNC_TYPE_MNEU.equals(func.getType())
					|| ISysFuncService.FUNC_TYPE_MODULE.equals(func.getType())) {
				funcMenuList.add(func);
			}
		}
		/* 递归子菜单 */
		for (FuncTree tree : funcTree.getChildFunc()) {
			listFuncMenu(tree, funcMenuList);
		}

	}

	/**
	 * 列表构建系统功能[整个功能树]
	 * 
	 * @param funcTree
	 *            ：功能树对象
	 * @param funcList
	 *            ：系统功能列表
	 */
	public static void listFunc(FuncTree funcTree, List<SysFunc> funcList) {

		SysFunc func = null;
		for (FuncTree tree : funcTree.getChildFunc()) {
			func = tree.getFunc();
			funcList.add(func);
		}
		/* 递归子菜单 */
		for (FuncTree tree : funcTree.getChildFunc()) {
			listFunc(tree, funcList);
		}

	}

	/**
	 * json构建菜单[只有模块和菜单信息]
	 * 
	 * @param funcTree
	 *            ：功能树对象
	 * @param info
	 *            ：json数据缓存对象
	 * @throws JSONException
	 */
	public static JSONArray printFuncMenu(FuncTree funcTree,
			JSONArray jsonMembers) throws JSONException {
		SysFunc func = funcTree.getFunc();

		for (FuncTree tree : funcTree.getChildFunc()) {
			func = tree.getFunc();
			if (ISysFuncService.FUNC_TYPE_MNEU.equals(func.getType())
					|| ISysFuncService.FUNC_TYPE_MODULE.equals(func.getType())) {
				JSONObject member = new JSONObject();
				member.put("id", func.getFuncId());
				member.put("pId", func.getPid() == null ? 0 : func.getPid());
				member.put("name", func.getFuncName());
				member.put("funcType", func.getType());
				member.put("open", true);
				member.put("funcIcon", func.getIcon());

				if (ISysFuncService.FUNC_TYPE_MNEU.equals(func.getType())) {
					member.put("url", func.getFuncUrl());
				}

				jsonMembers.add(member);
			}
		}
		/* 递归子菜单 */
		for (FuncTree tree : funcTree.getChildFunc()) {
			printFuncMenu(tree, jsonMembers);
		}

		return jsonMembers;
	}

	/**
	 * json构建系统功能[整个功能树]
	 * 
	 * @param funcTree
	 *            ：功能树对象
	 * @param info
	 *            ：json数据缓存对象
	 * @throws JSONException
	 */
	public static JSONArray printFunc(FuncTree funcTree, JSONArray jsonMembers)
			throws JSONException {

		SysFunc func = null;

		for (FuncTree tree : funcTree.getChildFunc()) {
			func = tree.getFunc();
			JSONObject member = new JSONObject();
			member.put("id", func.getFuncId());
			member.put("pId", func.getPid() == null ? 0 : func.getPid());
			member.put("pName", funcTree.getFunc().getFuncName());
			member.put("name", func.getFuncName());
			member.put("open", false);
			member.put("funcType", func.getType());
			member.put("funcAcce", func.getAcceType());
			member.put("funcOrder", func.getSeq());
			member.put("funcUrl", func.getFuncUrl());
			member.put("funcIcon", func.getIcon());

			jsonMembers.add(member);
		}
		/* 递归子菜单 */
		for (FuncTree tree : funcTree.getChildFunc()) {
			printFunc(tree, jsonMembers);
		}

		return jsonMembers;
	}

	/**
	 * 构建岗位功能授权树
	 * @param funcTree
	 * @param funcIdList
	 * @param jsonMembers
	 * @return
	 * @throws JSONException
	 */
	public static JSONArray printFuncCheck(FuncTree funcTree,
			List<Integer> funcIdList, JSONArray jsonMembers) throws JSONException {

		SysFunc func = null;

		for (FuncTree tree : funcTree.getChildFunc()) {
			func = tree.getFunc();
			JSONObject member = new JSONObject();
			member.put("id", func.getFuncId());
			member.put("pId", func.getPid() == null ? 0 : func.getPid());
			member.put("pName", funcTree.getFunc().getFuncName());
			member.put("name", func.getFuncName());
			member.put("open", false);
			member.put("funcType", func.getType());
			member.put("funcAcce", func.getAcceType());
			member.put("funcOrder", func.getSeq());

			if (funcIdList.contains(func.getFuncId())) {
				member.put("funcCheck", true);
			} else {
				member.put("funcCheck", false);
			}

			jsonMembers.add(member);
		}

		/* 递归子菜单 */
		for (FuncTree tree : funcTree.getChildFunc()) {
			printFuncCheck(tree, funcIdList, jsonMembers);
		}

		return jsonMembers;
	}
	
	
}
